package com.example.combwx.mvp.view;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.example.combwx.R;
import com.example.combwx.mvp.base.model.BaseEntity;
import com.example.combwx.mvp.base.view.BaseActivity;
import com.example.combwx.mvp.contract.RegisterContract;
import com.example.combwx.mvp.model.RegisterModel;
import com.example.combwx.mvp.presenter.RegisterPresenter;
import com.example.combwx.mvp.view.fragment.AccountFragment;
import com.example.combwx.mvp.view.fragment.PhoneFragment;

import java.util.Map;

import io.reactivex.Observable;
import okhttp3.RequestBody;

public class RegisterActivity extends
        AppCompatActivity implements View.OnClickListener {

    private android.widget.RelativeLayout loginLinear;
    private android.widget.ImageView loginBack;
    private android.widget.LinearLayout fragmentVesselParent;
    private android.widget.LinearLayout fragmentVessel;
    private android.widget.TextView loginSwitch;
    private AccountFragment accountFragment;
    private PhoneFragment phoneFragment;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();
        accountFragment = new AccountFragment();
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.add(R.id.fragment_vessel,accountFragment).commit();

    }

    public void initView() {
        loginLinear = (RelativeLayout) findViewById(R.id.login_linear);
        loginBack = (ImageView) findViewById(R.id.login_back);
        fragmentVesselParent = (LinearLayout) findViewById(R.id.fragment_vessel_parent);
        fragmentVessel = (LinearLayout) findViewById(R.id.fragment_vessel);
        loginSwitch = (TextView) findViewById(R.id.login_switch);
        loginBack.setOnClickListener(this::onClick);
        loginSwitch.setOnClickListener(this::onClick);
    }

    private void editlmg(int p){
        Drawable leftDrawable = null;
        leftDrawable  = getResources().getDrawable(p);
        leftDrawable.setBounds(0,0,leftDrawable.getMinimumWidth()
        ,leftDrawable.getMinimumHeight());
        loginSwitch.setCompoundDrawables(leftDrawable,null,null,null);

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.login_switch:
                FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
                AnimatorSet animatorSet = new AnimatorSet();
                ObjectAnimator translationY = ObjectAnimator.ofFloat(fragmentVesselParent, "translationY", 1, 300);
                ObjectAnimator alpha = ObjectAnimator.ofFloat(fragmentVesselParent, "alpha", 1, 0);
                ObjectAnimator translationY2 = ObjectAnimator.ofFloat(fragmentVesselParent, "translationY", 300, 1);
                ObjectAnimator alpha2 = ObjectAnimator.ofFloat(fragmentVesselParent, "alpha", 0, 1);
                animatorSet.setDuration(800);
                animatorSet.playTogether(translationY,alpha,translationY2,alpha2);
                animatorSet.start();
                if (loginSwitch.getText().toString().trim().equals("手机登录")){
                    loginSwitch.setText("账号登录");
                    editlmg(R.mipmap.res_7icon_364);
                    if (phoneFragment == null){
                        phoneFragment = new PhoneFragment();
                        fragmentTransaction.add(R.id.fragment_vessel,phoneFragment);
                    }
                    fragmentTransaction.hide(accountFragment);
                    fragmentTransaction.show(phoneFragment);
                }else {
                    loginSwitch.setText("手机登录");
                    editlmg(R.mipmap.res_7icon_363);
                    fragmentTransaction.hide(phoneFragment);
                    fragmentTransaction.show(accountFragment);
                }
                fragmentTransaction.commit();
                break;
            case R.id.login_back:
                finish();
                break;
        }

    }
}
